package com.example.service.impl;

import com.example.common.RedisCommon;
import com.example.common.RedisCommonEnum;
import com.example.entity.User;
import com.example.exception.BizAssertUtils;
import com.example.jwt.JwtUtil;
import com.example.mapper.UserMapper;
import com.example.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.util.Md5Util;
import com.example.util.RedisCacheUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2023-02-28
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisCacheUtil redisCacheUtil;
    @Override
    public String login(String account, String password) {
        User user = userMapper.queryUserByAccount(account);
        BizAssertUtils.notNull(user,"账号或密码错误");
        Boolean decrypt = Md5Util.decrypt(user.getPassword(), password);
        BizAssertUtils.isTrue(decrypt,"账号或密码错误");
        String token = JwtUtil.createJWT(account);
        redisCacheUtil.set(RedisCommon.getRedisKey(RedisCommonEnum.ACCOUNT_TOKEN_KEY,account),token,30L);
        return token;
    }
}
